package com.kla.power.controller;

import com.kla.power.config.LogPointer;
import com.kla.power.entity.MerchantEntity;
import com.kla.power.entity.UserEntity;
import com.kla.power.service.IMerchantService;
import com.kla.power.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商户/部门表 前端控制器
 * </p>
 * @author WuZH
 * @since 2023-06-19
 */
@RestController
@RequestMapping ("/merchant")
@CrossOrigin (value = "*", maxAge = 3600)
public class MerchantController {

    @Value ("${filePath}")
    private String filePath;
    @Resource
    private IMerchantService iMerchantService;

    @RequestMapping ("queryList")
    @LogPointer ("查询商户列表")
    public ResultBody queryList (@RequestBody MerchantEntity merchantEntity) {
        // 获取当前登录用户信息
        UserEntity userEntity = (UserEntity) RedisUtils.get ( merchantEntity.getToken () );
        merchantEntity.setId ( userEntity.getMerchantId () );
        List<MerchantEntity> merchantEntities = iMerchantService.queryList ( merchantEntity );
        // 树结构不分页，数量就是集合的size
        return ResultBody.success ( merchantEntities, merchantEntities.size () );
    }

    @RequestMapping ("querySingle")
    @LogPointer ("查询商户详情")
    public ResultBody querySingle (@RequestBody MerchantEntity merchantEntity) {
        merchantEntity = iMerchantService.querySingle ( merchantEntity );
        return ResultBody.success ( merchantEntity );
    }

    @RequestMapping ("saveSingle")
    @LogPointer ("保存商户")
    public ResultBody saveSingle (@RequestBody MerchantEntity merchantEntity) {
        merchantEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        merchantEntity = iMerchantService.saveSingle ( merchantEntity );
        return ResultBody.success ( merchantEntity );
    }

    @RequestMapping ("queryMerchant")
    @LogPointer ("查询当前登录商户")
    public ResultBody queryMerchant (@RequestBody MerchantEntity merchantEntity) {
        // 获取当前登录用户信息
        UserEntity userEntity = (UserEntity) RedisUtils.get ( merchantEntity.getToken () );
        merchantEntity.setId ( userEntity.getMerchantId () );
        MerchantEntity merchant = iMerchantService.querySingle ( merchantEntity );
        return ResultBody.success ( merchant );
    }

    /**
     * 修改商户部门信息
     * @param merchantEntity
     * @return
     */
    @RequestMapping ("updateSingle")
    @LogPointer ("更新商户信息")
    public ResultBody updateSingle (@RequestBody MerchantEntity merchantEntity) {
        merchantEntity = iMerchantService.updateSingle ( merchantEntity );
        return ResultBody.success ( merchantEntity );
    }

    @RequestMapping ("deleteSingle")
    @LogPointer ("删除商户")
    public ResultBody deleteSingle (@RequestBody MerchantEntity merchantEntity) {
        merchantEntity.setIsUsed ( 0 );
        merchantEntity = iMerchantService.updateSingle ( merchantEntity );
        return ResultBody.success ( merchantEntity );
    }

    @RequestMapping ("uploadFile")
    public ResultBody uploadLogo (MultipartFile multipartFile, String id) {
        String fileName = FileUtil.fileNameTreated ( multipartFile.getOriginalFilename () );
        try {
            FileUtil.upload ( multipartFile, filePath, fileName );
        } catch ( Exception e ) {
            e.printStackTrace ();
        }
        MerchantEntity merchantEntity = new MerchantEntity ();
        merchantEntity.setId ( id );
        merchantEntity.setLogo ( ConstUtil.DO_MAIN + "/img/" + fileName );
        merchantEntity = iMerchantService.updateSingle ( merchantEntity );
        return ResultBody.success ( merchantEntity );
    }

}
